/*
 * SPLogout
 * 
 * Version information
 * 
 * Apr 10, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.management.webapp.struts.action;

import static net.amsoft.iservice.util.IServiceConstants.DEBUG;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.webapp.dataObject.LoginData;
import net.amsoft.iservice.isso.client.ISSOClient;
import net.amsoft.iservice.isso.client.SAMLDataObject;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.ResolverClient;

import org.apache.soap.Envelope;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * @author mohit
 * 
 */
public class SPLogout extends BaseAction {

    public ActionForward process(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
            throws Exception {
        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + F_ENTRY);

        /* ********* Remove session attributes *********** */
        oRequest.getSession().removeAttribute("ServerName");
        oRequest.getSession().removeAttribute("ServerPath");
        oRequest.getSession().removeAttribute("Service");
        oRequest.getSession().removeAttribute("Links");
        oRequest.getSession().removeAttribute("Banner");
        /* *********************************************** */

        LoginData oLoginData = (LoginData) oRequest.getSession(false)
                .getAttribute(IBrokerConstants.SESSION_LOGIN_OBJ);

        ActionMessages oMsgs = new ActionErrors();

        String sSAMLArtifactDoc = oRequest
                .getParameter(ISSOClient.PARAM_SAML_ART);

        String sRelayState = oRequest
                .getParameter(ISSOClient.PARAM_RELAY_STATE);

        SAMLDataObject oReceivedSAMLData = ISSOClient
                .getValuesFromSAMLArtifact(sSAMLArtifactDoc);

        if (!sRelayState.equals(oLoginData.getRelayState())
                || !oLoginData.getProviderIDHash().equals(
                        oReceivedSAMLData.getProviderIDHash())) {
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.ERR_UKNOWN_GLOBAL_SESSION));
            saveErrors(oRequest, oMsgs);
            IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + 
                    "invalid RelayState or Artifact");
            IServiceUtil.log(DEBUG, this, F_PROCESS + "redirect to=" + 
            		IBrokerConstants.FWD_CHKLOGIN);
            return oMapping.findForward(IBrokerConstants.FWD_CHKLOGIN);
        }

        String sArtifactConsumerService = ResolverClient.getMetaDataIDP(
                oLoginData.getIDPMetaDataEP()).getArtifactResolutionServiceURL();

        String sArtifactResolveXML = ISSOClient
                .createSAMLArtifactResolveRequest(oReceivedSAMLData
                        .getArtifact());

        Envelope oEnvelope = ISSOClient.createSOAPEnvelope(sArtifactResolveXML);
        Envelope oReceivedEnvelope = ISSOClient.sendAndReceiveSOAPEnvelope(
                oEnvelope, sArtifactConsumerService);

        IServiceUtil.log(IServiceConstants.INFO, this, F_PROCESS + 
                "soap envelope received: " + oReceivedEnvelope);

        String sArtifactResponseXML = ISSOClient
                .getArtifactResponseFromSOAPEnvelpe(oReceivedEnvelope);

        String sSAMLStatus = ISSOClient
                .getLogoutSatusFromArtifactResponse(sArtifactResponseXML);

        IServiceUtil.log(IServiceConstants.WARN, this, F_PROCESS + 
                "received STATUS:" + sSAMLStatus);

        if (!ISSOClient.SAML_STATUS_SUCCESS.equals(sSAMLStatus)) {
            IServiceUtil.log(IServiceConstants.DEBUG, this, F_PROCESS + 
                    "message=" + IBrokerConstants.ERR_UKNOWN_GLOBAL_SESSION);
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.ERR_UKNOWN_GLOBAL_SESSION));
            saveErrors(oRequest.getSession(false), oMsgs);
        } else {
            IServiceUtil.log(IServiceConstants.DEBUG, this, F_PROCESS + 
                    "message=" + IBrokerConstants.MSG_LOGOUT_SUCCESS);
            oMsgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    IBrokerConstants.MSG_LOGOUT_SUCCESS));
            saveErrors(oRequest.getSession(false), oMsgs);
        }
        
        IServiceUtil.log(IServiceConstants.DEBUG, this, F_PROCESS + 
                "redirect to=" + IBrokerConstants.FWD_SUCCESS);
        return oMapping.findForward(IBrokerConstants.FWD_SUCCESS);
    }
}
